package frame;

import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.ComponentUI;

import java.awt.event.*;

public class MyInternalFrame extends JInternalFrame implements ActionListener {

	// Implement Action of Button
	static final private String CREATETABLE = "create_table_click";
	private JFrame frame;
	// Variable of InternalFrame
	static final int xOffset = 110, yOffset = 110;

	public MyInternalFrame(int openFrameCount) {

		super("Doc #" + openFrameCount, true, true, false, false);
		setBackground(Color.WHITE);
		setSize(xOffset, yOffset);
		setLocation(openFrameCount + xOffset, openFrameCount + yOffset);
		
	    
		// Crate toolbar
		createToolBar();
		setVisible(true);
	}

	protected void createToolBar() {
		JToolBar toolBar = new JToolBar();

		// addbutton
		addButtons(toolBar);// paramitor is JToolbar

		setLayout(new BorderLayout());
		add(toolBar, BorderLayout.NORTH);
	}

	protected void addButtons(JToolBar toolBar/* Referent of Toolbar */) {
		
		JButton button = null;

		// Create a Table //img name //Action //Tooltip //Action Value
		button = makeNavigationButton("database_table", 
									  CREATETABLE,
									  "Create a Table.", 
									  "create_table_click");
		toolBar.add(button);
	}

	protected JButton makeNavigationButton( String imageName,
											String actionCommand, 
											String toolTipText, 
											String altText) {
		// Look for the image.
		String imgLocation = imageName + ".png";

		// Create and initialize the button.
		JButton button = new JButton();
		button.setActionCommand(actionCommand);
		button.setToolTipText(toolTipText);
		button.addActionListener(this);

		if (imgLocation != null) {// image found
			button.setIcon(new ImageIcon(imgLocation, altText));
		} else {// no image found
			button.setText(altText);
			System.err.println("Resource not found: " + imgLocation);
		}
		return button;
	}

	public void actionPerformed(ActionEvent e) {
		// Handle each button.
		if ("create_table_click".equals(e.getActionCommand())) { // first button
			// clicked
			Table aTable = new Table();
			add(aTable);
			setSize(400, 170);
		}
	}

}
